import grace from "../../utils/grace.js"
grace.page({
      data: {
            params: {
                  status: 0, //0未使用 1已经使用 2已失效
                  pageCurrent: 1,
                  pageSize: 20
            },
            hasMore: false,
            list: [],
            receive_code: '',
            isChoose: 0
      },
      onLoad: function(options) {
            console.log(options);
            let param = options[0];
            if (param) {
                  if (param[0]['isChoose']) {
                        this.$data.isChoose = param[0]['isChoose'];
                  }
            }
            this.loadData();
      },
      loadData: function() {
            this.$http.get('api/coupon/user_coupon_list', this.$data.params)
                  .then((res) => {
                        let hasMore = res.list.length >= this.$data.params.pageSize;
                        const data = this.$data.params.pageCurrent == 1 ? res.list : this.$data.list.concat(res.list);
                        this.$data = {
                              list: data,
                              hasMore: hasMore
                        };
                  });
      },
      onPullDownRefresh: function() {
            this.$data.params.pageCurrent = 1;
            this.loadData();
      },
      onReachBottom: function() {
            if (this.$data.hasMore) {
                  ++this.$data.params.pageCurrent;
                  this.$data.hasMore = false;
                  this.loadData();
            }
      },
      switchTab: function(e) {
            var cur = e.target.dataset.status;
            if (this.$data.params.status != cur) {
                  this.$data.params.status = cur;
                  this.$data.params.pageCurrent = 1;
                  this.loadData();
            }
      },
      exchange: function() {
            let receive_code = this.$data.receive_code;
            if (receive_code.length == 0) {
                  this.toast('请先填写领取吗！');
                  return;
            }
            let from_ad = '线下兑换';
            let that = this;
            this.$http.post('api/coupon/receive', {
                        receive_code: receive_code,
                        from_ad: from_ad
                  })
                  .then((res) => {
                        this.toast('兑换成功！');
                        this.$data.receive_code = '';
                        setTimeout(function() {
                              that.loadData();
                        }, 800);
                  });
      },
      inputHandle: function(e) {
            let name = e.target.dataset.name;
            this.$data[name] = e.detail.value;
      },
      clearInput: function() {
            this.$data.receive_code = '';
      },
      chooseBack: function(e) {
            let coupon_id = e.currentTarget.dataset.cid;
            if (this.$data.isChoose) {
                  this.$goBack({
                        coupon_id: coupon_id
                  });
            } else {
                  wx.switchTab({
                        url: '../goods/goods',
                  })
            }
      }
})